package com.tuyan.aixiang.component;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tuyan.aixiang.MainApplication;
import com.tuyan.aixiang.R;
import com.tuyan.aixiang.common.lang.VoidFunction;
import com.tuyan.aixiang.model.YsDevice;
import com.tuyan.aixiang.util.StringUtil;
import com.tuyan.aixiang.util.ToastUtil;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZOpenSDK;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class VidItem extends RelativeLayout {

    private static final String TAG = "VidItem";

    @BindView(R.id.iv_capture)
    ImageView ivCapture;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.iv_more)
    ImageView ivMore;

    @BindView(R.id.tv_online_status)
    TextView tvOnlineStatus;

    @BindView(R.id.tv_dynamic)
    TextView tvDynamic;

    Boolean refreshing = false;

    private YsDevice ysDevice;

    public VidItem(Context context) {
        this(context, null);
    }

    public VidItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VidItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = LayoutInflater.from(context).inflate(R.layout.vid_item, this);
        ButterKnife.bind(contentView, this);
    }

    public VidItem setup(YsDevice device) {
        ysDevice = device;
        tvTitle.setText(ysDevice.getDeviceName());
        if (device.getOnlineStatus().equals(1)) {
            tvOnlineStatus.setText("在线");
            tvOnlineStatus.setTextColor(getContext().getResources().getColor(R.color.colorAccent));
        } else {
            tvOnlineStatus.setText("不在线");
            tvOnlineStatus.setTextColor(getContext().getResources().getColor(R.color.colorDanger));
        }

        if (device.getDynamic().equals(0)) {
            tvDynamic.setVisibility(GONE);
        } else if (device.getDynamic().equals(1)) {
            tvDynamic.setVisibility(VISIBLE);
            tvDynamic.setTextColor(getContext().getResources().getColor(R.color.colorPrimary));
            tvDynamic.setText("注册中");
        } else if (device.getDynamic().equals(2)) {
            tvDynamic.setVisibility(VISIBLE);
            tvDynamic.setTextColor(getContext().getResources().getColor(R.color.colorDanger));
            tvDynamic.setText("注册失败");
        }
        refresh();
        return this;
    }

    public void onMoreClick(VoidFunction function) {
        ivMore.setOnClickListener(v -> {
            function.call();
        });

    }

    public void onCoverClick(VoidFunction function) {
        ivCapture.setOnClickListener(v -> {
            function.call();
        });
    }

    public void refresh() {
        if (refreshing) return;
        refreshing = true;
//        if(null == ysDevice.getDevicePicUrl()) {
//        } else {
//            Glide.with(MainApplication.getAppContext()).load(ysDevice.getDevicePicUrl()).into(ivCapture);
//        }

        Observable.create((Observable.OnSubscribe<String>) subscriber -> {
            try {
                String url = EZOpenSDK.getInstance().captureCamera(ysDevice.getDeviceSerial(), ysDevice.getChannelNo());
                subscriber.onNext(url);
            } catch (BaseException e) {
                e.printStackTrace();
                subscriber.onNext(null);
            } finally {
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        refreshing = false;
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
                        if (StringUtil.isBlank(s)) {
//                            ToastUtil.makeShort("刷新视频封面失败");
                            ivCapture.setImageResource(R.drawable.default_camera_capture);
//                            Glide.with(MainApplication.getAppContext()).load(ysDevice.getDevicePicUrl()).into(ivCapture);
                        } else {
                            Glide.with(MainApplication.getAppContext()).load(s).into(ivCapture);
                        }
                    }
                });

//        new Thread(() -> {
//            try {
//                // TODO  ysDevice.getChannelNo()
//                String url = EZOpenSDK.getInstance().captureCamera(ysDevice.getDeviceSerial(),1);
//                Glide.with(MainApplication.getAppContext()).load(url).into(ivCapture);
//            } catch (BaseException e) {
//                e.printStackTrace();
//            } finally {
//                refreshing = false;
//            }
//        }).start();

    }

}
